package demo.main;

import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.ctab;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.report.builder.crosstab.CrosstabBuilder;
import net.sf.dynamicreports.report.builder.crosstab.CrosstabColumnGroupBuilder;
import net.sf.dynamicreports.report.builder.crosstab.CrosstabRowGroupBuilder;
import net.sf.dynamicreports.report.constant.Calculation;
import net.sf.dynamicreports.report.constant.PageOrientation;
import net.sf.dynamicreports.report.constant.PageType;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

public class Crosstab2 extends MutiReport {
	@At("/getCrosstab2")
	@Ok("json")
	public String getCrosstab2(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("getCrosstab2");
		build(request, response);
		return resultResult;
	}

	@Override
	protected void build(HttpServletRequest request, HttpServletResponse response) {
		CrosstabRowGroupBuilder<String> rowGroup = ctab.rowGroup("state",
				String.class).setTotalHeader("合计");

		CrosstabColumnGroupBuilder<String> columnGroup = ctab.columnGroup(
				"item", String.class);

		CrosstabBuilder crosstab = ctab
				.crosstab()
				.headerCell(
						cmp.text("地区 / 产品").setStyle(
								Templates.boldCenteredStyle))
				.rowGroups(rowGroup)
				.columnGroups(columnGroup)
				.measures(
						ctab.measure("数量", "quantity", Integer.class,
								Calculation.SUM),
						ctab.measure("单价", "unitprice",
								BigDecimal.class, Calculation.SUM));

		try {
			reportBuilder = report().setPageFormat(PageType.A4, PageOrientation.LANDSCAPE)
					.setTemplate(Templates.reportTemplate)
					.title(Templates.createTitleComponent(""))
					.summary(crosstab).pageFooter(Templates.footerComponent)
					.setDataSource(createDataSource());
			setResult(reportBuilder.toJasperPrint(), request, response);
		} catch (DRException e) {
			e.printStackTrace();
		}
	}

	private JRDataSource createDataSource() {
		DataSource dataSource = new DataSource("state", "item", "quantity",
				"unitprice");
		dataSource.add("北京", "Notebook", 1, new BigDecimal(500));
		dataSource.add("北京", "DVD", 5, new BigDecimal(30));
		dataSource.add("北京", "DVD", 2, new BigDecimal(45));
		dataSource.add("北京", "DVD", 4, new BigDecimal(36));
		dataSource.add("北京", "DVD", 5, new BigDecimal(41));
		dataSource.add("北京", "Book", 2, new BigDecimal(11));
		dataSource.add("北京", "Book", 8, new BigDecimal(9));
		dataSource.add("北京", "Book", 6, new BigDecimal(14));

		dataSource.add("纽约", "Notebook", 1, new BigDecimal(610));
		dataSource.add("纽约", "DVD", 4, new BigDecimal(40));
		dataSource.add("纽约", "DVD", 6, new BigDecimal(35));
		dataSource.add("纽约", "DVD", 3, new BigDecimal(46));
		dataSource.add("纽约", "DVD", 2, new BigDecimal(42));
		dataSource.add("纽约", "Book", 3, new BigDecimal(12));
		dataSource.add("纽约", "Book", 9, new BigDecimal(8));
		dataSource.add("纽约", "Book", 4, new BigDecimal(14));
		dataSource.add("纽约", "Book", 5, new BigDecimal(10));

		dataSource.add("上海", "Notebook", 1, new BigDecimal(460));
		dataSource.add("上海", "DVD", 3, new BigDecimal(49));
		dataSource.add("上海", "DVD", 4, new BigDecimal(32));
		dataSource.add("上海", "DVD", 2, new BigDecimal(47));
		dataSource.add("上海", "Book", 4, new BigDecimal(11));
		dataSource.add("上海", "Book", 8, new BigDecimal(6));
		dataSource.add("上海", "Book", 6, new BigDecimal(16));
		dataSource.add("上海", "Book", 3, new BigDecimal(18));
		return dataSource;
	}

}
